;+
;PURPOSE (***DEPRECATED**)
;	to read in histogram output from SLUG (currently deprecated!!!!)
;SYNTAX
;	read_slug_histo, infile, outfile=outfile
;INPUTS
;	infile: name of slug output file
;	outfile: name for fits output file [default='slug_histo.fits']
;OUTPUTFILE
;	a fits file that is two arrays of structures (one at each extension)
;_____________________________________________________________________________
; 	(extension 1) 2d histograms (all flux histograms are for logarithmic
;			abscissa)
;		histoname: name of the histogram
;		x_time_range: the range of times that make up the x-axis
;			of the 2d histogram
;		y_quan_range: the range of the quantity as named by the 
;			histoname tag
;		y_quan_mid: the midpoint of the bin (for ease of plotting)
;			note: the first bin has a midpoint set to 0 and
;			the last bin has a midpoint set to 99
;		tablebins: the 2d histogram
;	i.e. tablesbins[x,y] tells you the number of clusters that fell
;	  in the interval of y_quan_range[y] and y_quan_range[y+1] at the time
;	  step defined by x_time_range[x].
;_____________________________________________________________________________
;	(extension 2) 1d histograms 
;		name: the name of the histogram
;		range: the range for the bins
;		bins: the vaue for each bin (i.e. bins[i] contains
;			the number of clusters that fell between
;			range[i] and range[i+1]
;NOTE!!!!
;	the first and last bins have different sizes than all the others!
;	be careful. DO NOT think that the binsizes for all are something
;	like xrange[1]-xrange[0].... use xrange[2]-xrange[1] and be careful
;	with the first and last bins	
;
;Written by R. da Silva, UCSC, 3-15-10
;-
pro read_slug_histo, infile, outfile=outfile
if not keyword_set(infile) then begin
print, 'You did not specify an input file using a default... bad idea!!!'
infile='../output/histo_ddd'
endif

histonames=['NUV', $ 	;the names of the histogram
'FUV', $		;this code is written to be able
'BOL', $		;to easily allow more tables if they are written
'HA', $			;in the SLUG output file. All one would need	
'NUV/FUV',$		;to do is add the name to this list (BE SURE
'NUV/BOL',$		;to place it in the correct order! It *MUST*
'FUV/BOL',$		;be the same as was written in the SLUG output file)
'HA/BOL',$
'HA/NUV',$
'HA/FUV']

if not keyword_set(outfile) then outfile='slug_histo.fits'
bit64=is64bit()				;is your machine 64bit?
if bit64 then longdt=14 else longdt=3 ;determine what your long datatype is
				      ;it can be different based on your architecture
openr, lun, infile, /get_lun		;open up the file
;---------------------------------------------------
;read in 2d histogram data one histogram at a time |
;---------------------------------------------------
for i=0, n_elements(histonames)-1 do begin
;parse the format of the output file
nx=(read_binary(lun,DATA_TYPE=longdt,DATA_DIMS=1))[0] ;number of x-axis elements
ny=(read_binary(lun,DATA_TYPE=longdt,DATA_DIMS=1))[0] ;number of y-axis elements
xrange=read_binary(lun,DATA_TYPE=5,DATA_DIMS=nx+1)    ;the xrange for the xaxis
yrange=read_binary(lun,DATA_TYPE=5,DATA_DIMS=ny+1)    ;the yrange for the yaxis
arr=read_binary(lun,DATA_TYPE=5,DATA_DIMS=nx*ny)      ;the actual 2d table
; we previously incorrectly used arr=reform(arr, nx, ny)			      ;this was our mistake... something to do with C having different
      ;x, y, coordinates
arr=reform(arr, ny, nx)			      ;reformat the array
mid= (yrange+shift(yrange, -1))/2.                    ;define midpoints
mid[0]=0
mid[ny-1]=99
mid=mid[0:ny-1]
xrange=xrange[1:*];
if i EQ 0 then begin				;write in tidy structure
   str={histoname:histonames[i], $
   time_range:xrange, $
   quan_range:yrange, $
   quan_mid:mid, $
   tablebins:arr}
endif else begin
str2={histoname:histonames[i], $
   time_range:xrange, $
   quan_range:yrange, $
   quan_mid:mid, $
   tablebins:arr}
str=[str, str2]
endelse
endfor
mwrfits, str, outfile, /create			;write out file

;read in 1d histogram
n=(read_binary(lun,DATA_TYPE=longdt,DATA_DIMS=1))[0];number of elements
range=read_binary(lun,DATA_TYPE=5,DATA_DIMS=n+1) ;read in abscissa
arr=read_binary(lun,DATA_TYPE=5,DATA_DIMS=n)  ;the array of values per bin

mid= (range+shift(range, -1))/2.                    ;define midpoints
mid[0]=0
mid[ny-1]=99
mid=mid[0:ny-1]

str1d={name:'mass histogram', $			;tidy structrure
range:range, $
mid:mid, $
bins:arr}

mwrfits, str1d, outfile				;write file
close, lun ;close the file
end
